﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Devtm.Parsing
{

    public class ItemBase
    {

        public virtual string Parse(Lexer l, Context c, string _result)
        {

            string result = _result;

            Token t;

            while ((t = l.Next()) != Token.Eof)
            {

                switch (t)
                {
                    case Token.OpenBrace:
                        result = VisitOpenBrace(l, c, result);
                        break;

                    case Token.CloseBrace:
                        result = VisitCloseBrace(l, c, result);
                        break;

                    case Token.DoublePoint:
                        result = VisitDoublePoint(l, c, result);
                        break;

                    case Token.Text:
                        result = VisitText(l, c, result);
                        break;

                }

            }

            return result;

        }

        protected virtual string VisitOpenBrace(Lexer l, Context c, string value)
        {
            var f = new ItemEvaluateFormula();
            string result = f.Parse(l, c, value);
            return result;
        }

        protected virtual string VisitCloseBrace(Lexer l, Context c, string value)
        {
            return value;
        }

        protected virtual string VisitDoublePoint(Lexer l, Context c, string value)
        {
            return Parse(l, c, value);
        }

        protected virtual string VisitText(Lexer l, Context c, string value)
        {
            return value + l.Value;
        }

    }

}
